﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WaveSim.Core.Equations.Galerkin;
using WaveSim.Core.Methods.ODE;
using WaveSim.Core.Research;
using System.IO;
using MicroWave;

namespace WaveSim.Core.Test
{
    [TestClass]
    public class Galerkin2D
    {
        private Wave wave;
        private WaveGuide waveGuide;
        private Galerkin2DEquation eq;

        public Galerkin2D()
        {
            wave = new Wave
            {
                F0 = 8 * Math.Pow(10, 9)
            };

            waveGuide = new WaveGuide(0.023, 0.010, 1);

            //eq = new Galerkin2DEquation(wave, waveGuide, null, 100);

            Rectangle rect1 = new Rectangle
            {
                ReEps = 3.2,
                ImEps = 0,
                X0 = 0.000,
                X1 = 0.010,
                Z0 = 0.01,
                Z1 = 0.03
            };

            Rectangle rect2 = new Rectangle
            {
                ReEps = 3.2,
                ImEps = 0,
                X0 = 0.013,
                X1 = 0.023,
                Z0 = 0.01,
                Z1 = 0.03
            };

            eq = new Galerkin2DEquation(wave, waveGuide, new Composite(new List<BaseDielectric> { rect1, rect2 }), 100);
        }

        [TestMethod]
        public void TestBalance()
        {
            FieldCalculator calculator = new FieldCalculator(eq, new Runge4());
            var amps = eq.StartValues(new double[] { 1 });
            var results = calculator.Get(amps, 0.1, 0, 100000);
            var r = eq.Rejection(results);
            var t = eq.Transmission(amps, r, results);
            var b = eq.Balanse(r, t);
        }

        [TestMethod]
        public void TestDispersion()
        {
            Dispersion disp = new Dispersion(eq, new Runge4(), 0.1, 0, 10000);
            var dispRes = disp.Get(.1 * Math.Pow(10, 9), 15 * Math.Pow(10, 9), 100);
            string dir = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\').TrimEnd('/') + "\\dispDiel1.txt";

            using (FileStream file = new FileStream(dir, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(file))
                {
                    double prev = dispRes[0, 1];
                    //if (prev < 0) prev += Math.PI;
                    for (int i = 0; i < dispRes.GetLength(0); i++)
                    {
                        //while (dispRes[i, 1] < prev)
                        {
                            //dispRes[i, 1] += Math.PI;
                        }
                        prev = dispRes[i, 1];
                        writer.WriteLine("{0}\t{1}", dispRes[i, 0], dispRes[i, 1]);
                    }
                }
            }
        }
    }
}
